#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, d[10001], q[10001], f[10001];
vector<ll> edge[10001];

inline void Toposort() {
	ll front = 1, rear = 0;
	for (ll i = 1; i <= n; ++i)
		if (!d[i])
			q[++rear] = i;
	while (front <= rear) {
		ll x = q[front];
		++front;
		for (auto y : edge[x])
			if (--d[y] == 0)
				q[++rear] = y;
	}
	for (ll i = n; i; i--) {
		ll x = q[i];
		f[x] = 100;
		for (auto y : edge[x])
			f[x] = max(f[x], f[y] + 1);
	}
	ll ans = 0;
	
	for (ll i = 1; i <= n; ++i) {
		ans += f[i];
		printf("%lld %lld\n", i, f[i]);
	}
		
	printf("%lld\n", ans);
}

int main() {
	scanf("%lld%lld", &n, &m);
	for (ll i = 1; i <= m; ++i) {
		ll x, y;
		scanf("%lld%lld", &x, &y);
		edge[x].push_back(y);
		++d[y];
	}
	Toposort();
	return 0;
}
